// component/dqjj/dqjj.js
var app = getApp();
var config = require("../../utils/config.js");
var network = require("../../utils/network.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled',
    dqjjData: [], //电器家具数据 
    dqjjItem: [],  //电器家具选项数据
    dpPackage: [],  //电器家具大类数据
    tcPrice: 0, //有选择套餐价格
    url:app.globalData.url
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getDqjjData: function () {
      network.request({
        url: config.host + 'api_work/furnitureList',
        method: "POST",
        data: { cateId: 3 },
        success: (res) => {
          console.log("电器家具数据")
          console.log(res)
          console.log(res.data.data.package)
          if (res.data.code == 99) {
            this.setData({
              dqjjData: res.data.data,
              dqjjItem: res.data.data.cate,
              dpPackage: res.data.data.package
            })
            console.log(this.data.dqjjItem)
            this.triggerEvent("changeYw",0)
          } else {
            wx.showToast({
              title: res.data.msg,
            })
          }

        }
      })
    },
    changeTx: function (e) {
      let packageid = e.currentTarget.dataset.packageid  //选中的大套餐id
      let index = e.currentTarget.dataset.choose  //选中项的下标
      let list = this.data.dpPackage
      let idParams = []
      if (list[index].status == 1) {
        list[index].status = 0
      } else {
        list[index].status = 1
      }
      this.setData({
        dpPackage: list
      })
      //  for (let i in list){
      //    if(list[i].status){
      //       idParams.push(list[i].id)
      //    }
      //  }
      wx.request({
        url: config.host + 'api_work/furnitureAlgorithm',
        method: "POST",
        data: { cateId: 3, cate: this.data.dqjjItem, package: this.data.dpPackage },
        success: (res) => {
          console.log("电器家具数据")
          console.log(res.data.data.package)
          console.log(res.data.data.cate)
          if (res.data.code == 99) {
            this.setData({
              dqjjData: res.data.data,
              dqjjItem: res.data.data.cate,
              // dpPackage:res.data.data.package
              //tcPrice:res.data.data.money
            })
            this.tcPrice();
            console.log("我选择了套餐的价格")
            console.log(this.data.tcPrice)
            wx.setStorageSync("ywzw", { "name": "衣物织物皮具", "moeny": this.data.tcPrice, "id": 2, "data": { "package": this.data.dpPackage, "cate": this.data.dqjjItem, } })
            this.triggerEvent("changeYw", this.data.tcPrice)
          } else {
            wx.showToast({
              title: res.data.msg,
            })
          }

        }
      })

    },
    /* 点击减号 */
    bindMinus: function (e) {
      console.log(e)
      let num = e.currentTarget.dataset.num   //默认的见数
      let index = e.currentTarget.dataset.index //下标
      let pid = e.currentTarget.dataset.pid  //上级id
      let min = e.currentTarget.dataset.min   //可选择最小值
      let tcnum = e.currentTarget.dataset.tcnum  //套餐最小值
      let list = this.data.dqjjItem
      let list1 = this.data.dpPackage  //大类套餐数据
      let id = e.currentTarget.dataset.id //id点击事件
      let totalPrice = this.data.tcPrice //套餐价格
      num--;
      if (tcnum > num) {
        for (let p in list1) {
          console.log(list1[p].cate)
          if (list1[p].cate) {
            if (list1[p].cate.indexOf(id) != '-1') {
              console.log(list1[p].status)
              list1[p].status = 0
            }
          }
        }

        this.setData({
          dpPackage: list1,
        })
      }
      this.tcPrice()
      if (num > min || num == min) {
        for (let i in list) {
          if (list[i].id == pid) {
            list[i].cate[index].default = num
            this.setData({
              dqjjItem: list
            })
          }
        }
      }
      this.addPrice();
    },
    /* 点击加号 */
    bindPlus: function (e) {
      console.log(e)
      let num = e.currentTarget.dataset.num   //默认的见数
      let index = e.currentTarget.dataset.index //下标
      let pid = e.currentTarget.dataset.pid  //上级id
      let list = this.data.dqjjItem
      num++;
      for (let i in list) {
        if (list[i].id == pid) {
          list[i].cate[index].default = num
          this.setData({
            dqjjItem: list
          })
        }
      }
      this.addPrice();
    },
    /* 输入框事件 */
    bindManual: function (e) {
      console.log(e)
      let num = e.detail.value;
      //默认的见数
      let index = e.currentTarget.dataset.index //下标
      let pid = e.currentTarget.dataset.pid  //上级id
      let list = this.data.dqjjItem
      let list1 = this.data.dpPackage  //大类套餐数据
      let id = e.currentTarget.dataset.id //id点击事件
      let totalPrice = this.data.tcPrice //套餐价格
      let tcnum = e.currentTarget.dataset.tcnum  //套餐最小值
      console.log("输入")
      console.log(tcnum)
      if (tcnum > num) {
        for (let p in list1) {
          console.log(list1[p].cate)
          if (list1[p].cate) {
            if (list1[p].cate.indexOf(id) != '-1') {
              console.log(list1[p].status)
              list1[p].status = 0
            }
          }
        }
        this.setData({
          dpPackage: list1,
        })
      }
      this.tcPrice()
      for (let i in list) {
        if (list[i].id == pid) {
          list[i].cate[index].default = num
          this.setData({
            dqjjItem: list
          })
        }
      }
      this.addPrice();
    },
    // 价格计算
    addPrice: function () {
      let dpPackage = this.data.dpPackage  //大类数据
      let dqjjItem = this.data.dqjjItem  //小类数据
      let totalPrice = this.data.tcPrice;
      let isTc = 0   //是否套餐
      for (let o in dpPackage) {
        console.log(dpPackage[0].status)
        if (dpPackage[o].status == 1) {
          isTc = 1
        }
      }
      console.log(isTc)
      if (isTc == 0) {
        totalPrice = 0
        //客户没有选择套餐
        console.log("我要单算")
        for (let i in dqjjItem) {
          for (let n in dqjjItem[i].cate) {
            totalPrice += dqjjItem[i].cate[n].price * dqjjItem[i].cate[n].default
          }
        }
      } else {
        //客户选择了套餐
        totalPrice = 0
        for (let o in dpPackage) {
          if (dpPackage[o].status == 1) {
            totalPrice += dpPackage[o].price
          }
        }
        let cate = []  //选择的套餐包含的小项目id
        for (let j in dpPackage) {
          if (dpPackage[j].status == 1) {
            cate = cate.concat(dpPackage[j].cate)
          }
        }
        console.log(cate)
        let item = []  //先找出小项目件数不等于0的项目
        let itemid = []  //小项目件数不等于0的项目id
        for (let k in dqjjItem) {
          for (let m in dqjjItem[k].cate) {
            if (dqjjItem[k].cate[m].default != 0) {
              item.push(dqjjItem[k].cate[m])
              itemid.push(dqjjItem[k].cate[m].id)
            }
          }
        }
        console.log("不等于0的项目")
        console.log(item)
        console.log(itemid)
        console.log(this.includes(cate, itemid))
        let different = this.getArrDifference(itemid, cate) //不同的数据
        if (different.lenght != 0) {
          /**
           * 有不同的数据 不同的数据 按照单个项目计算
           */
          console.log("有不同数据")
          console.log("套餐价格" + totalPrice)
          console.log(different)
          for (let m in different) {
            for (let z in item) {
              if (item[z].id == different[m]) {
                console.log("不同项目的算法")
                console.log(item[z].price * item[z].default)
                totalPrice += item[z].price * item[z].default
              }
            }
          }
        }
        console.log(totalPrice)
        /** 
           * 循环选中的项目
           * 如果全部包含 判断小项目里面数量是否大于默认数量
           * 如果大于price且没有在不同的项目内 + 超出部分的值
           * 如果等于 就是套餐价格 prcie
           * 
           * **/

        for (let a in item) {
          if (item[a].num < item[a].default) {
            console.log("这是没有不同项目的算法")
            if (different.indexOf(item[a].id) == -1) {
              totalPrice += (item[a].default - item[a].num) * item[a].price
            }
          }
        }
      }
      console.log("最终价格")
      console.log(totalPrice)
      this.setData({
        tcPrice: totalPrice
      })
      // 将客户选中的套餐及项目价格存入缓存
      wx.setStorageSync("ywzw", { "name": "衣物织物皮具", "moeny": this.data.tcPrice, "id": 2, "data": { "package": this.data.dpPackage, "cate": this.data.dqjjItem, } })
      this.triggerEvent("changeYw", this.data.tcPrice)
    },
    //判断一个数组是否包含另外一个数组
    includes: function (arr1, arr2) {
      return arr2.every(val => arr1.includes(val));
    },
    //取出两个数组相通的元素
    getArrDifference(arr1, arr2) {

      return arr1.concat(arr2).filter(function (v, i, arr) {

        return arr.indexOf(v) === arr.lastIndexOf(v);

      });

    },
    //套餐价格
    tcPrice: function () {
      let num = 0; //总价 
      let list3 = this.data.dpPackage
      for (let i in list3) {
        if (list3[i].status == 1) {
          num += list3[i].price
        }
      }
      this.setData({
        tcPrice: num
      })

    }

  }
})
